package com.shuqi.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.Util;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.ThirdAccountUnbindParser;

public class ThirdAccountUnbindModel extends ModelBase implements HttpRequestListener {

    private HttpRequest mHttpRequest;

    private Context mContext;

    public ThirdAccountUnbindModel(Context context) {
        this.mContext = context;
    }

    @Override
    public void action(int actionCode, Object object) {
        fireEvent(actionCode, object);
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        return null;
    }

    @Override
    public String getRequestUrl() {
        return Urls.getThirdAccountUnbindUrl();
    }

    @Override
    public void onDestroy() {
        if (mHttpRequest != null) {
            mHttpRequest.abort();
        }
    }

    /**
     * 
     * @param type 第三方类型
     * @param opentId 第三方key
     */
    public void unBind(String uid, final int type, final String opentId) {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        String key = "c56cf32e9a52a265ae47cd50570266cc";
        params.add(new BasicNameValuePair("userid", uid)); // 用户id
        params.add(new BasicNameValuePair("tptype", type + ""));// 第三方账户类型
        params.add(new BasicNameValuePair("tpid", opentId));// 第三方账户key
        long timestamp = System.currentTimeMillis();
        String strTimestamp = timestamp + "";
        String md5_key_sb = key + timestamp;
        String md5_key = Util.MD5(md5_key_sb.toString());
        params.add(new BasicNameValuePair("key", md5_key));// md5
        params.add(new BasicNameValuePair("timestamp", strTimestamp));// 时间戳
        mHttpRequest =
                new HttpRequest(mContext, UrlManager.COLUMN_DOMAIN_ACCOUNT, getRequestUrl(), params, this);
        mHttpRequest.setDataParseAdapter(new ThirdAccountUnbindParser());
        MyTask.runInBackground(mHttpRequest, true);
    }

    @Override
    public void loadData(Object... objs) {}

}
